package com.hs.srm.admin.modules.material.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.hs.srm.base.common.utils.EntityUtils;
import com.hs.srm.base.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.hs.srm.admin.modules.material.entity.MaterialEntity;
import com.hs.srm.admin.modules.material.entity.MaterialFactoryEntity;
import com.hs.srm.admin.modules.material.service.MaterialFactoryService;
import com.hs.srm.admin.modules.material.service.MaterialService;
import com.hs.srm.admin.modules.sys.controller.AbstractController;
import com.hs.srm.base.common.annotation.SysLog;
import com.hs.srm.common.utils.PageUtils;
import com.hs.srm.common.utils.R;
import com.hs.srm.common.validator.ValidatorUtils;
import com.hs.srm.common.validator.group.UpdateGroup;
/**
 * 物料的信息
 *
 * @author yanfei
 * @date 2019年1月14日
 */
@RestController
@RequestMapping("/material")
public class MaterialController extends AbstractController {

    private static final long serialVersionUID = -4123823577744890422L;


    // 注入业务层
    @Autowired
    private MaterialService materialService;
    @Autowired
    private MaterialFactoryService materialFactoryService;


    /**
     * 保存物料的信息
     */
    @SysLog("保存物料信息")
    @RequestMapping("/save")
    public R save(MaterialEntity materialEntity, MaterialFactoryEntity materialFactoryEntity) {
        String maxCode = this.materialService.selectMaterialMaxCode();
        maxCode = "N" + (Integer.valueOf(maxCode.replaceAll("N", "")) + 1);
        if (materialEntity != null && materialFactoryEntity != null) {
            materialFactoryEntity.setCrossMaterialStatus("Z2");
            materialFactoryEntity.setStatus("DELBVQG");
            //materialFactoryEntity.setMaterialCode(maxCode);
            materialFactoryEntity.setBeginDate(materialEntity.getEffectiveDate());
            //materialEntity.setMaterialCode(maxCode);
            this.materialService.insertMaterial(materialEntity);
            this.materialFactoryService.insertMaterial(materialFactoryEntity);
            return R.ok();
        }
        return R.error();
    }

    /**
     * 查询供应商信息
     */
    @ResponseBody
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        // 查询用户列表
        PageUtils page = this.materialService.queryPage(params);
        List<MaterialEntity> list = (List<MaterialEntity>) page.getList();
        list = EntityUtils.valToTxt(list);
        page.setList(list);
        return R.ok(page);
    }

    /**
     * @Description 删除物料信息
     * @Param [pkId]
     * @Return org.springframework.web.servlet.ModelAndView
     * @Author socik
     * @Date 2019/5/23 11:37
     */
    @SysLog("删除物料信息")
    @ResponseBody
    @RequestMapping("/delete")
    public R toDelete(@RequestParam(value = "materialCode[]") String[] materialCode) {
        List<String> idList = Arrays.asList(materialCode);
        idList.forEach(pkid ->{
            this.materialService.deleteMaterialByCode(pkid);
        });
        return R.ok();
    }

    /**
     * 更新供应商信息
     */
    @SysLog("修改物料信息")
    @RequestMapping("/update")
    public R update(MaterialEntity materialEntity) {
        List<MaterialFactoryEntity> materialFactoryEntities=this.materialFactoryService.selectMaterialFactoryListByCode(materialEntity.getMaterialCode());
        if(materialFactoryEntities!=null){
            materialFactoryEntities.forEach(materialFactoryEntity ->{
                materialFactoryEntity.setMaterialDesc(materialEntity.getMaterialDesc());
                this.materialFactoryService.updateById(materialFactoryEntity);
            });
        }
        ValidatorUtils.validateEntity(materialEntity, UpdateGroup.class);
        // 根据id进行更新，没有传值的属性就不会更新
        this.materialService.updateById(materialEntity);
        return R.ok();
    }

    /**
     * @Description 跳转到编辑页面获取登陆用户名
     * @Param []
     * @Return org.springframework.web.servlet.ModelAndView
     * @Author socik
     * @Date 2019/5/27 11:55
     */
    @RequestMapping("/toEdit/{materialCode}")
    public ModelAndView toEdit(@PathVariable("materialCode") String materialCode) {
        MaterialEntity materialEntity = this.materialService.selectMaterialByCode(materialCode);
        // 把查询出来的对象赋值给物料信息实体
        ModelAndView modelAndView = new ModelAndView("modules/inforecord/material_edit");
        modelAndView.addObject("materialEntity", materialEntity);
        return modelAndView;
    }

    /**
     * @Description 跳转到新增页面获取登陆用户名
     * @Param []
     * @Return org.springframework.web.servlet.ModelAndView
     * @Author socik
     * @Date 2019/5/27 11:55
     */

    @RequestMapping("/toAdd")
    @RequiresPermissions("material:add")
    public ModelAndView toAdd() {
        SysUserEntity username = this.getUser();
        // 把查询出来的对象赋值给物料信息实体
        ModelAndView modelAndView = new ModelAndView("modules/inforecord/material_add");
        modelAndView.addObject("username", username.getUsername());
        return modelAndView;
    }

    /**
     * 根据物料编码获取物料数据
     * @param materialCode
     * @return
     */
    @GetMapping("/getMaterialByCode")
    public R getMaterialByCode(String materialCode){
        MaterialEntity materialEntity = materialService.selectMaterialByCode(materialCode);
        return R.ok(materialEntity);
    }
}
